package com.weishaoying.consumer.config;

import com.meituan.service.mobile.mtthrift.client.pool.MTThriftPoolConfig;
import com.meituan.service.mobile.mtthrift.proxy.ThriftClientProxy;
import com.weishaoying.api.service.UserThriftService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author WeiShaoying
 * @date 2018/9/27
 */
@Configuration
public class JavaConfig {

    @Bean(name = "thriftPoolConfig")
    public MTThriftPoolConfig getMTThriftPoolConfig() {
        MTThriftPoolConfig thriftPoolConfig = new MTThriftPoolConfig();
        thriftPoolConfig.setMaxActive(100);
        thriftPoolConfig.setMaxIdle(20);
        thriftPoolConfig.setMinIdle(5);
        thriftPoolConfig.setMaxWait(3000);
        thriftPoolConfig.setTestOnBorrow(true);
        thriftPoolConfig.setTestOnReturn(false);
        thriftPoolConfig.setTestWhileIdle(false);
        return thriftPoolConfig;
    }

    @Bean(name = "userThriftService", destroyMethod = "destroy")
    public ThriftClientProxy getThriftClientProxy(MTThriftPoolConfig thriftPoolConfig) {
        ThriftClientProxy thriftClientProxy = new ThriftClientProxy();
        thriftClientProxy.setMtThriftPoolConfig(thriftPoolConfig); // [可选]配置
        thriftClientProxy.setServiceInterface(UserThriftService.class); // [MUST]接口类
        thriftClientProxy.setAppKey("com.sankuai.octo.testMTthrift.client"); // [MUST]服务消费者 appkey
        thriftClientProxy.setRemoteAppkey("com.sankuai.octo.testMTthrift"); // [MUST]服务提供者 appkey
        thriftClientProxy.setRemoteServerPort(9461); // [常用]服务提供者 port
        thriftClientProxy.setTimeout(30000); // [常用]调用超时
        return thriftClientProxy;
    }

}
